from WSJob import WSJob
from Registry import Registry

class BackupsFounderJob(WSJob):#TODO MongoJob

    def __init__(self):
        super().__init__()

    def fill(self, links):
        links = list(set(links))

        result = []

        dirs_exts  = Registry.get('config')['backups']['dirs'].split(',')
        files_exts = Registry.get('config')['backups']['files'].split(',')
        ignore_exts = Registry.get('config')['backups']['ignore_exts'].split(',') #FIXME вычищать пробелы

        for link in links:
            if link == '/': continue
            if link[-1:] == '/':
                link = link.split("/")
                del link[-1]
                last = link[-1]
                del link[-1]

                for dir_ext in dirs_exts:
                    tmp = dir_ext.replace('|name|', last).strip()
                    tmp = "/".join(link) + "/" + tmp
                    result.append(tmp if tmp[0] == '/' else "/" + tmp)
            else:
                ext = link.split(".")[-1]
                if ext in ignore_exts:
                    continue

                link = link.split("/")
                last = link[-1:][0]
                del link[-1:]

                for file_ext in files_exts:
                    tmp = file_ext.replace('|name|', last).strip()
                    tmp = "/".join(link) + "/" + tmp
                    result.append(tmp if tmp[0] == '/' else "/" + tmp)

        for link in result:
            self.put(link)

        return self.qsize()
